
var feedSummary = {
	lasttime:"",
	totalinfo:"",
	feedcount:1,
	init:function(){
		var data = $cache.read("logininfo");
		var lonininfo = JSON.parse(data);
		var json = {
			logininfo:lonininfo
		}
		summer.showProgress({
            "title" : "加载中..."
        });
		$service.callAction({
			"viewid" : "com.sunnercn.feed.SuppliesRequestApplyController", //后台带包名的Controller名
			"action" : "query", //方法名,
			"params" : json, //自定义参数
			"callback" : "callBack()", //请求回来后执行的ActionID
			"error" : "erresg()"//失败回调的ActionId
		});
	},
	bindEvent:function(){
		$("#summaryback").unbind().on("click",function(){
			summer.closeWin();
		});
		$("#takeback").unbind().on("click",function(){
			//收回按钮点击
			if($(this).hasClass("um-btn-gray")){
				alert("汇总申请已审批，不可收回");
				return;
			}else{
				var billinfo= {
					pk_sum:$(this).attr("pk_sum")
				}
				var json= {
					billinfo:billinfo
				}
				summer.showProgress({
		            "title" : "加载中..."
		        });
				$service.callAction({
					"viewid" : "com.sunnercn.feed.SuppliesRequestApplyController", //后台带包名的Controller名
					"action" : "delete", //方法名,
					"params" : json, //自定义参数
					"callback" : "deletecallBack()", //请求回来后执行的ActionID
					"error" : "deleteerresg()"//失败回调的ActionId
				});
					
				
			}
		});
		$("#summary").unbind().on("click",function(){
			//汇总按钮点击
			if($(this).hasClass("um-btn-gray")){
				alert("汇总申请不可重复提交！");
				return;
			}else{
				var logininfoStr = $cache.read("logininfo");
				var logininfo = JSON.parse(logininfoStr);
				var count = logininfo.henneryinfo.silo_num;
				if(count != feedSummary.feedcount){
					alert("所申请数量不对，请刷新后重试");
					return;
				}
				var billinfo={
					totalinfo: feedSummary.totalinfo,
					feedinfo:{
						lasttime: feedSummary.lasttime
					}
				};
				var json= {
					billinfo:billinfo,
					logininfo:logininfo
				}
				summer.showProgress({
		            "title" : "加载中..."
		        });
				$service.callAction({
					"viewid" : "com.sunnercn.feed.SuppliesRequestApplyController", //后台带包名的Controller名
					"action" : "add", //方法名,
					"params" : json, //自定义参数
					"callback" : "addcallBack()", //请求回来后执行的ActionID
					"error" : "adderresg()"//失败回调的ActionId
				});
			}
		});
		$("#summaryupdate").unbind().on("click",function(){
			feedSummary.init();
		});
		
		
	},
	loadpage:function(data){
	
		if(data.feedinfo &&data.feedinfo.length>0){
			//1.存储请求结构体
			feedSummary.totalinfo=data.totalinfo;
			feedSummary.lasttime=data.feedinfo[0].ts;
			feedSummary.feedcount=data.feedinfo.length;
			//2.初始化
			feedSummary.initList(data.feedinfo);
			feedSummary.inittotalinfo(data.totalinfo);
			//3.状态判断
			if(data.statusinfo.pk_sum){
				$("#takeback").attr("pk_sum",data.statusinfo.pk_sum);
			}
			if(data.statusinfo.total_status=="1"){
				$("#summary").addClass("um-btn-gray");
			}else{
				$("#summary").removeClass("um-btn-gray");
			}
			if(data.statusinfo.total_status=="0"||data.statusinfo.approves_status=="1"){
				$("#takeback").addClass("um-btn-gray");
			}else{
				$("#takeback").removeClass("um-btn-gray");
			}
		}else{
			 //TODO 无数据界面
		}
	},
	inittotalinfo:function(data){
		$("#totalinfo").html("");
		var html ='<li><div class="um-list-item"><div class="um-list-item-inner">'
				 +'	<div class="um-list-item-left um-border-right">料号</div><div class="um-list-item-right">'
				 +'		<p>数量</p></div></div></div></li>';
		for(var i=0;i<data.length;i++){
				html +='<li><div class="um-list-item"><div class="um-list-item-inner">'
					 + '	<div class="um-list-item-left um-border-right">'+data[i].feed_type_name+'</div><div class="um-list-item-right">'
				 	 + '		<p>'+data[i].feed_num+'吨</p></div></div></div></li>';
		}
		
		$("#totalinfo").append(html);
		
	},
	initList:function(data){
		var height = $(".um-content").height();
		var footH = $("#totalinfo").height();
		var liH = height-footH;
		$("#listview").height(liH);
	//构造控件实例
        $("#listview").html("");
        var html = '<ul class="um-list um-no-active" >';
		for(var i = 0;i<data.length;i++){
			html +='<li class="um-listview-row">'
				 + '<div class="um-list-item-inner">'
				 + '	<div class="um-list-item-body"style=" padding-left: 10px;">'
				 + '		<p class="um-media-heading fb f16 um-text-overflow">'+data[i].silo_name+'</p>'
				 + '		<p class="um-gray f14 um-text-overflow">'+data[i].feed_type_name+'</p>'
				 + '	</div>'
				 + '	<div class="pr10">'
				 + '		重量：<span class="um-gray f14">'+data[i].feed_num+'</span>吨'
				 + '	</div>'
				 + '</div></li>';
		}
		html += '</ul>';			
		$("#listview").append(html);		   		
	}

};
/**
 * 接口回调模块 
 */
function callBack(arg){
	summer.hideProgress();
	alert(JSON.stringify(arg));
	if(arg.status == "0"){
		feedSummary.loadpage(arg.data);
	}else{
		alert(arg.message);
	}	
}
function erresg(arg){
	summer.hideProgress();
	alert(JSON.stringify(arg));
}
// 保存接口
function addcallBack(arg){
	summer.hideProgress();
	alert(JSON.stringify(arg));
	if(arg.status == "0"){
		$("#takeback").attr("pk_sum",arg.data.pk_sum);
		$("#summary").addClass("um-btn-gray");
		$("#takeback").removeClass("um-btn-gray");
		
	}else{
		alert(arg.message);
	}	
}
function adderresg(arg){
	summer.hideProgress();
	alert("失败");
	alert(JSON.stringify(arg));
}
function deletecallBack(arg){
	summer.hideProgress();
	alert(JSON.stringify(arg));
	if(arg.status == "0"){
		
		$("#takeback").addClass("um-btn-gray");
		$("#summary").removeClass("um-btn-gray");
		
	}else{
		alert(arg.message);
	}
	
}
function deleteerresg(arg){
	summer.hideProgress();
	alert("失败");
	alert(JSON.stringify(arg));
}
summerready = function () {    
    
    feedSummary.init();
	feedSummary.bindEvent();
	
};